﻿package net.wangit.jdbc.database;

import net.wangit.jdbc.JDBCException;

public class Sybase extends Database
{

	public Sybase()
	{
		registerType(1, 1, "CHAR($p)");
		registerType(2, 12, "VARCHAR($p)");
		registerType(11, 5, "SMALLINT");
		registerType(12, 4, "INT");
		registerType(13, 2, "NUMERIC(19,0)");
		registerType(21, 6, "FLOAT");
		registerType(22, 8, "DOUBLE PRECISION");
		registerType(23, 2, "NUMERIC($p, $s)");
		registerType(31, 91, "DATETIME");
		registerType(32, 92, "DATETIME");
		registerType(33, 93, "DATETIME");
		registerType(41, 2005, "TEXT");
		registerType(43, 2004, "IMAGE");
	}

	public int getTypeCode(int sqlTypeCode, String columnTypeName, int precision, int scale)
		throws JDBCException
	{
		switch (sqlTypeCode)
		{
		case 1: // '\001'
			return 1;

		case 12: // '\f'
			return 2;

		case 5: // '\005'
			return 11;

		case 4: // '\004'
			return 12;

		case -5: 
			return 13;

		case 2: // '\002'
			return scale <= 0 ? 13 : 23;

		case 6: // '\006'
			return 21;

		case 8: // '\b'
			return 22;

		case 91: // '['
		case 92: // '\\'
		case 93: // ']'
			return 33;

		case 2005: 
			return 41;

		case -4: 
		case 2004: 
			return 43;
		}
		throw new JDBCException((new StringBuilder("Unsupported sqlType: sqlTypeCode=")).append(sqlTypeCode).append(",columnTypeName=").append(columnTypeName).toString());
	}

	public int getDefaultBatchSize()
	{
		return 15;
	}

	public String getAddColumnString()
	{
		return "add";
	}

	public String getNullColumnString()
	{
		return " null";
	}

	public boolean supportsForUpdate()
	{
		return false;
	}

	public boolean supportsIdentityColumns()
	{
		return true;
	}

	public String getIdentitySelectString()
	{
		return "select @@identity";
	}

	public String getIdentityColumnString()
	{
		return "IDENTITY NOT NULL";
	}

	public String getNoColumnsInsertString()
	{
		return "DEFAULT VALUES";
	}

	public char closeQuote()
	{
		return ']';
	}

	public char openQuote()
	{
		return '[';
	}

	public String appendIdentitySelectToInsert(String insertSQL)
	{
		return (new StringBuilder(String.valueOf(insertSQL))).append("\nselect @@identity").toString();
	}
}
